package com.apac.jmesa;

import java.util.TreeMap;

import org.jmesa.view.html.event.RowEvent;

public class RowEventJmesa implements RowEvent {
	
	private String inputId;
	private String primaryKey;
	private String arrayName; //For multiple rows
	private Boolean isMultiple;
	private JmesaEvents jevents;
	
	public RowEventJmesa(String inputId, String primaryKey, JmesaEvents jevents) {
		super();
		this.inputId = inputId;
		this.primaryKey = primaryKey;
		this.jevents = jevents;
		this.isMultiple = false;
	}
	
	public RowEventJmesa(String inputId, String primaryKey, JmesaEvents jevents, String arrayName) {
		super();
		this.inputId = inputId;
		this.primaryKey = primaryKey;
		this.arrayName = arrayName;
		this.jevents = jevents;
		this.isMultiple = true;
	}

	public String getInputId() {
		return inputId;
	}

	public void setInputId(String inputId) {
		this.inputId = inputId;
	}

	public String getPrimaryKey() {
		return primaryKey;
	}

	public void setPrimaryKey(String primaryKey) {
		this.primaryKey = primaryKey;
	}
	
	public String getArrayName() {
		return arrayName;
	}

	public void setArrayName(String arrayName) {
		this.arrayName = arrayName;
	}

	public String getSelectRow(String id) {
		return "selectRow(this,'"+id+"','"+getInputId()+"');";
	}
	
	public String getMultipleRow(String id) {
		return "selectRow(this, "+this.arrayName+" ,'"+id+"','"+getInputId()+"');";
	}

	@SuppressWarnings("rawtypes")
	@Override
	public String execute(Object arg0, int arg1) {
				
		String id = (String) ((TreeMap) arg0).get(getPrimaryKey());
		
//		System.out.println("from RowEventJmesa.execute, id: "+id);
//		System.out.println("from RowEventJmesa.execute, ok: "+getPrimaryKey());
		
		this.jevents.setId(id);
		
		String toExecute = "";
		if(isMultiple){
			toExecute = getMultipleRow(id);
		}else{
			toExecute = getSelectRow(id);
		}
		
		for(int x = 0; x < jevents.getEvents().size(); x++){
			
			if(jevents.getEvents().get(x).equals("ajaxEmployee")){
				toExecute = toExecute + jevents.doAjaxSelectEmp();
			}else if(jevents.getEvents().get(x).equals("showEmployeeFile")){
				toExecute = toExecute + jevents.doShowEmployeeFile();
				
			}
			
		}
		
		return toExecute;
	}
	
	

}
